package testlib.concurrent;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;


/**
 * 锁中断
 * @author Kwok
 * 2023-07-09
 */
public class Test_Lock_Interruptibly implements Runnable {

	private Lock lock = new ReentrantLock();

	public static void main(String[] args) {

		Test_Lock_Interruptibly test_Lock_Interruptibly = new Test_Lock_Interruptibly();
		Thread thread0 = new Thread(test_Lock_Interruptibly);
		Thread thread1 = new Thread(test_Lock_Interruptibly);
		thread0.start();
		thread1.start();

		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
		thread1.interrupt();
	}

	@Override
	public void run() {
		System.out.println(Thread.currentThread().getName() + "尝试获取锁");
		try {
			lock.lockInterruptibly();
			try {
				System.out.println(Thread.currentThread().getName() + "获取到了锁");
				Thread.sleep(5000);
			} catch (InterruptedException e) {
				System.out.println(Thread.currentThread().getName() + "睡眠期间被中断了");
			} finally {
				lock.unlock();
				System.out.println(Thread.currentThread().getName() + "释放了锁");
			}
		} catch (InterruptedException e) {
			System.out.println(Thread.currentThread().getName() + "获得锁期间被中断了");
		}
	}
}
